package com.ptswitch.material.service.impl;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.hxzon.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.ptswitch.material.dao.impl.ContractItemDao;
import com.ptswitch.material.model.contract.ContractItem;
import com.ptswitch.material.service.ContractItemService;

@Service
public class ContractItemServiceImpl implements ContractItemService {

    private static final Logger logger = LoggerFactory.getLogger(ContractItemServiceImpl.class);
    @Resource
    private ContractItemDao contractItemDao;

    public List<ContractItem> findAll(long first, long size) {
        return contractItemDao.findAll(first, size);// " order by p.createTime desc "
    }

    public List<ContractItem> findByExamine(String id, boolean pass) {
        if (pass) {
            return contractItemDao.findPassByContractId(id);//" where p.numPass=p.numReceiving and p.contract.id='"
        } else {
            return contractItemDao.findNotPassByContractId(id);//" where p.numPass!=p.numReceiving and p.contract.id='"
        }
    }

    public List<ContractItem> findByReceiving(String id, boolean receiving) {
        if (receiving) {
            return contractItemDao.findReceivingByContractId(id);//" where p.numReceiving =p.quantities and p.contract.id='"
        } else {
            return contractItemDao.findNotReceivingByContractId(id);//" where p.numReceiving !=p.quantities and p.contract.id='"
        }
    }

    public List<ContractItem> findByReturn(String id) {
        return contractItemDao.findReturnByContractId(id);//" where p.numPass!=p.numReceiving and p.contract.id='"
    }

    public List<ContractItem> findByContract(String id) {
        return contractItemDao.findByContractId(id);// " where p.contract.id='"
    }

    public boolean existContractItemCode(ContractItem item) {
        // " where p.contract.id='"
        //"' and p.code='"
        ContractItem result = contractItemDao.existContractItemCode(item.getContract().getId(), item.getCode());
        return result != null;
    }

    private Pattern p = Pattern.compile(".*-\\d*-(\\d*)");

    public String emptyCode(String code) {
        code = StringUtil.escapeSql(code);
        //"select p.code from ContractItem as p where p.code like '"
        //"-%' order by p.code"
        List<String> list = (List<String>) contractItemDao.findByCode(code);// code-%
        if (list.isEmpty()) {
            return code + "-1";
        }
        String lastCode = list.get(list.size() - 1);
        logger.debug("get empty code:{}", lastCode);
        Matcher m = p.matcher(lastCode);
        try {
            m.matches();
            int index = Integer.parseInt(m.group(1)) + 1;
            return code + "-" + index;
        } catch (RuntimeException e) {
            logger.error("get empty code fail", e);
        }
        return code;
    }

    @Override
    public void saveAll(List<ContractItem> items) {
        contractItemDao.saveAll(items);
    }

    @Override
    public void save(ContractItem item) {
        contractItemDao.save(item);
    }

    @Override
    public void delete(ContractItem item) {
        contractItemDao.delete(item);
    }

    @Override
    public ContractItem findById(String id) {
        return contractItemDao.findById(id);
    }

}
